package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.BriefReportVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BriefReport;
import com.ruoyi.system.service.IBriefReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 简报模板Controller
 * 
 * @author ruoyi
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/system/template")
@Api(tags = "舆情：简报模板管理")
public class BriefReportController extends BaseController
{
    @Autowired
    private IBriefReportService briefReportService;

    /**
     * 查询简报模板列表
     */
    @ApiOperation("查询简报模板列表")
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(BriefReport briefReport)
    {
        startPage();
        briefReport.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<BriefReport> list = briefReportService.selectBriefReportList(briefReport);
        return getDataTable(list);
    }

    /**
     * 导出简报模板列表
     */
    @ApiOperation("导出简报模板列表")
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "简报模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BriefReport briefReport)
    {
        briefReport.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<BriefReport> list = briefReportService.selectBriefReportList(briefReport);
        ExcelUtil<BriefReport> util = new ExcelUtil<BriefReport>(BriefReport.class);
        return util.exportExcel(list, "简报模板数据");
    }

    /**
     * 获取简报模板详细信息
     */
    @ApiOperation("获取简报模板详细信息")
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{briefReportId}")
    public AjaxResult getInfo(@PathVariable("briefReportId") Long briefReportId)
    {
        return AjaxResult.success(briefReportService.selectBriefReportById(briefReportId));
    }

    /**
     * 新增简报模板
     */
    @ApiOperation("新增简报模板")
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "简报模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BriefReport briefReport)
    {
        briefReport.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        briefReport.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        briefReport.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        briefReport.setIsSystem(0);
        return toAjax(briefReportService.insertBriefReport(briefReport));
    }

    /**
     * 新增简报模板(包括文章栏目)
     */
    @ApiOperation("新增简报模板(包括文章栏目)")
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "简报模板", businessType = BusinessType.INSERT)
    @PostMapping("/createTemplate")
    public AjaxResult createTemplate(@RequestBody BriefReportVo briefReport)
    {
        briefReport.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        briefReport.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        briefReport.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        briefReport.setIsSystem(0);
        return toAjax(briefReportService.createTemplate(briefReport));
    }

    /**
     * 修改简报模板
     */
    @ApiOperation("修改简报模板")
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "简报模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BriefReport briefReport)
    {
        return toAjax(briefReportService.updateBriefReport(briefReport));
    }


    /**
     * 设置默认模板
     */
    @ApiOperation("设置默认模板")
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "简报模板", businessType = BusinessType.UPDATE)
    @GetMapping("/setDefaultTemplate/{briefReportId}")
    public AjaxResult setDefaultTemplate(@PathVariable("briefReportId") Long briefReportId)
    {
        return toAjax(briefReportService.setDefaultTemplate(briefReportId));
    }




    /**
     * 删除简报模板
     */
    @ApiOperation("删除简报模板")
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "简报模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{briefReportIds}")
    public AjaxResult remove(@PathVariable Long[] briefReportIds)
    {
        return toAjax(briefReportService.deleteBriefReportByIds(briefReportIds));
    }


    /**
     * 获取默认模板
     */
    @ApiOperation("获取默认模板")
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/getDefaultTemplate")
    public AjaxResult getDefaultTemplate()
    {
        return AjaxResult.success(briefReportService.getDefaultTemplate());
    }




}
